import { Guard, IGuard } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';

@Guard()
export class AuthGuard implements IGuard<Context> {
  async canActivate(context: Context): Promise<boolean> {
    if (!context.user) {
      throw new Error('Unauthorized: User not logged in');
    }

    if (context.user.validflag == 1) {
      return true;
    } 

    else if (context.user.validflag == 0) {
      throw new Error('Access denied: User not authorized');
    }
    
    throw new Error('Invalid user status');
  }
}